********************************************************************************
** Cederman, Galano, Girardin and Schvitz. War Did Make States.
** Article prepared for International Organization
** June 20, 2022
**
** Stata do-file: analysis_dyads.do
** Robustness analysis: Script for dyadic analysis with data from 1400
** Required file paths set in runall.do
********************************************************************************


cd $ROOT
cd $INTERMEDIATEDIR
use dyad_analysis2, clear
 
gen peaceXgrowthwara = (1-inc1)*llgrowthwara
gen peaceXgrowthpeacea = (1-inc1)*llgrowthpeacea
gen peaceXshrinkwarb = (1-inc1)*llshrinkwarb
gen peaceXshrinkpeaceb = (1-inc1)*llshrinkpeaceb

gen lrwara = l5.growthwara/(l5.llareaa)
gen lrXlr = lr * lr

gen peaceXllr = (1-inc1)*llr
gen initXllr = incinita1 * llr

replace llelevsda = 0 if llelevsda == .
replace llelevsdb = 0 if llelevsdb == .


label variable lnga "log terr. gain A"
label variable lneigh "neighbors AB"
label variable llnumneighsa "\\# neighbors A"
label variable llnumneighsb "\\# neighbors B"
label variable lldist "log distance AB"
label variable inc1 "war AB"
label variable lr "rel. size A/AB"
label variable lrXlr "rel. size ^2"
label variable llr "rel. log size A/AB"
label variable inc1Xllr "war X rel. log size"
label variable peaceXllr "peace X rel. log size"
label variable llareaa "log size A"
label variable lcoastala "Coastal access A"
label variable lcoastalb "Coastal access B"
label variable llagea "log age A"
label variable llageb "log age B"
label variable llareab "log size B"
label variable llelevsda "log elev. SD A"
label variable llelevsdb "log elev SD B"

label variable lurbana "urban share A"
label variable lurbanb "urban share B"

label variable lcentrala "core (A)"
label variable lcentralb "core (B)"


label variable llgrowthwara "cumul. war gains A"
label variable llgrowthpeacea "cumul. peace gains A"
label variable inc1Xgrowthwara "war X war gains A"
label variable inc1Xgrowthpeacea "war X peace gains A"

label variable peaceXgrowthwara "peace X war gains A"
label variable peaceXgrowthpeacea "peace X peace gains A"


label variable llshrinkwarb "cumul. war losses B"
label variable llshrinkpeaceb "cumul. peace losses B"
label variable inc1Xshrinkwarb "war X war losses B"
label variable inc1Xshrinkpeaceb "war X peace losses B"

label variable peaceXshrinkwarb "peace X war losses B"
label variable peaceXshrinkpeaceb "peace X peace losses B"

label variable lrgrowthwarab "cumul. war gains A/B"
label variable lrgrowthpeaceab "cumul. peace gains A/B"
label variable inc1Xgrowthwarab "war X war gains A/B"
label variable inc1Xgrowthpeaceab "war X peace gains A/B"

label variable lrshrinkwarba "cumul. war losses B/A"
label variable lrshrinkpeaceba "cumul. peace losses B/A"
label variable inc1Xshrinkwarba "war X war losses B/A"
label variable inc1Xshrinkpeaceba "war X peace losses B/A"

label variable incinita1 "A initiator"
label variable initXllr "A init. X rel. size"

label variable llgrowtha "cumul. gains A"
label variable llshrinkb "cumul. losses B"


// GENERATE TABLE A5
// DYADIC GAINS AND LOSSES BY CENTURY

eststo clear
global X "inc1Xgrowthwara peaceXgrowthwara inc1Xgrowthpeacea peaceXgrowthpeacea inc1Xshrinkwarb peaceXshrinkwarb inc1Xshrinkpeaceb peaceXshrinkpeaceb"
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb"
global TIMEVARS "noeventyear evspline*"

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year<=1490, absorb(year) cluster(idd2 ida idb)
estadd local period "1400-1490"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year>=1495 & year<=1590, absorb(year) cluster(idd2 ida idb)
estadd local period "1495-1590"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year>=1595 & year<=1690, absorb(year) cluster(idd2 ida idb)
estadd local period "1595-1690"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m3

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year>=1695, absorb(year) cluster(idd2 ida idb)
estadd local period "1695-1790"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m4

cd $ROOT
cd $OUTPUTDIR
estimates save "dyads.ster", replace

esttab m1 m2 m3 m4 using "dyads_4centuries.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains and losses by century \label{tab:dyadmain}") ///
order (inc1 $X  llareaa llareab) /// $CONTROLS) ///
keep (inc1 $X  llareaa llareab) /// $CONTROLS) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("period Period" "geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")
	  
	  


// GENERATE TABLE A11
// MAIN DYADIC MODELS FOR PAPER WITH DATA STARTING IN 1400

eststo clear
global X "inc1Xgrowthwara peaceXgrowthwara inc1Xgrowthpeacea peaceXgrowthpeacea inc1Xshrinkwarb peaceXshrinkwarb inc1Xshrinkpeaceb peaceXshrinkpeaceb"
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb"
global TIMEVARS "noeventyear evspline*"

reghdfe lnga0 inc1 llr  llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe lnga0 inc1 llr inc1Xllr   llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m3

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS, absorb(year idd) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m4

cd $ROOT
cd $OUTPUTDIR
estimates save "dyads.ster", replace

esttab m1 m2 m3 m4 using "dyads.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains of State A and losses of State B \label{tab:dyadmain}") ///
order (inc1 llr inc1Xllr $X  llareaa llareab $CONTROLS) ///
keep (inc1 llr inc1Xllr $X  llareaa llareab $CONTROLS) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")
